<?php

namespace backend\models;

use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
/**
 * 可以处理无限级分类的一个基类，把无限级的数据输出成select2或gridview所需要的格式
 *
 * @author zhuo bing
 */
class Node extends \yii\db\ActiveRecord {

    /**
     * 无限级返回select2所需要的数组
     * @param array $data 数组
     * @param int $parent 父id
     * @param string $tag 标签
     * @return array 格式化好的数组数据，可以直接用于select2
     */
    static public function getNodeSelect($data, $parent = null, $tag = '├') {
        $node = [];
        foreach ($data as $v) {
            if ($v['parent'] == $parent ) {
                $node[$v['id']] = Yii::$app->formatter->asRaw($tag . $v['name']);
                $subNode = self::getNodeSelect($data, $v['id'], '│┈┈ ' . $tag);
                $node = ArrayHelper::merge($node, $subNode);
            }
        }
        return $node;
    }
    /**
     * 无限级返回Gridview所需要的数组
     * @param array $data 数组
     * @param int $parent 父id
     * @param string $tag 标签
     * @return array 格式化好的数组数据
     */
    static public function getNodeGrid($data, $parent = null, $tag = '├') {
        $node = [];
        foreach ($data as $v) {
            if ($v['parent'] == $parent) {
                $v['name'] = $tag . $v['name'];
                $node[$v['id']] = $v;
                $subNode = self::getNodeGrid($data, $v['id'], '│&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $tag);
                $node = ArrayHelper::merge($node, $subNode);
            }
        }
        return $node;
    }
    /**
     * 判断是否有子节点
     * @param int $id
     * @return boolean
     */
    static public function isSubNode($id) {
        if (self::findOne(['parent' => $id])) {
            return true;
        }
        return false;
    }
    /**
     * 返回Ztree所需要的数据
     * @param type $data  树节点的所有数据
     * @param type $parent 树节点
     * @return array $nodes 格式化好的Ztree数据
     */
    static public function getZtreeNode($data,$currid = 0, $parent = null){
        $nodes = [];
        foreach($data as $v){
            if($v['parent'] == $parent){
                $node['name'] = $v['name'];
                $node['url'] = Url::to([$v['controller'].'/index','catid'=>$v['id']]);
                $node['target'] = '_self';
                $node['style'] = 'aaa';
                $subNode = self::getZtreeNode($data, $currid, $v['id']);
                if($subNode){
                    $node['open'] = 'true';
                    $node['children'] = $subNode;
                    //unset($node['url']);
                }
                $nodes[] = $node;
                unset($node);
            }
        }
        return $nodes;
    }
    /**
     * 返回树的面包屑
     * @param type $data   树的所有数据
     * @param type $parent 树节点
     * @param string $tag  面包屑的分格
     * @return string $breadcrumb 格式化好的面包屑
     */
    static public function getBreadcrumb($data,$parent,$tag ='&nbsp;<i class="fa fa-caret-right"></i>&nbsp;'){
        $breadcrumb = '';
        foreach($data as $v){
            if($v['id'] == $parent){
                $breadcrumb.=$v['name'];
                $parent = self::getBreadcrumb($data, $v['parent'], $tag);
                $breadcrumb = ($parent?$parent.$tag:'').$breadcrumb;
                unset($parent);
            }
        }
        return $breadcrumb;
    }
    /**
     * 读取一个节点的所有子节点ID
     * @param type 树节点数据
     * @param type 开始的节点ID
     */
    static public function getSubNodeId($data,$parent){
        $nodeIds = [];
        foreach($data as $v){
            if($v['parent'] == $parent){
                $nodeIds[] = $v['id'];
                $subNodeIds = self::getSubNodeId($data, $v['id']);
                $nodeIds = array_merge($nodeIds,$subNodeIds);
            }
        }
        return $nodeIds;
    }
}
